//
//  ChildDetailViewController.m
//  LoveDraw
//
//  Created by 梁晨 on 2017/6/26.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "ChildDetailViewController.h"
#import "ChildDetailTableViewCell.h"
#import "childDetailHeadV.h"
#import "UpRecordTableViewCell.h"
#import "GrowthRecordViewController.h"
#import "ChildDetailModel.h"
#import "EditchildInfoViewController.h"

@interface ChildDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)childDetailHeadV *headV;
@property(nonatomic,strong)UITableView *listTab;
@property(nonatomic,strong)ChildDetailModel *model;
@property(nonatomic,strong)UIImageView *backImageV;
@property(nonatomic,strong)UIButton *backBtn; // 返回按钮
@property(nonatomic,strong)UIView *backV;
@property(nonatomic,strong)UIButton *editBtn; //修改


@end

@implementation ChildDetailViewController

-(UITableView *)listTab
{
    if (!_listTab) {
        _listTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH) style:(UITableViewStyleGrouped)];
        _listTab.delegate = self;
        _listTab.dataSource = self;
        _listTab.separatorStyle = 0;
        _listTab.estimatedSectionHeaderHeight = 0;
        _listTab.estimatedSectionFooterHeight = 0;
        _listTab.backgroundColor = [UIColor clearColor];
        [_listTab registerNib:[UINib nibWithNibName:@"ChildDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"ChildDetailTableViewCell"];
        [_listTab registerNib:[UINib nibWithNibName:@"UpRecordTableViewCell" bundle:nil] forCellReuseIdentifier:@"UpRecordTableViewCell"];
    }
    return _listTab;
}

-(ChildDetailModel *)model
{
    if (!_model) {
        _model = [[ChildDetailModel alloc]init];
    }
    return _model;
}

-(UIImageView *)backImageV
{
    if (!_backImageV) {
        _backImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenW)];
    }
    return _backImageV;
}

-(UIView *)backV
{
    if (!_backV) {
        _backV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenW)];
        _backV.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    }
    return _backV;
}

-(UIButton *)backBtn
{
    if (!_backBtn)
    {
        _backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_backBtn setImage:[UIImage imageNamed:@"left"] forState:(UIControlStateNormal)];
        _backBtn.frame = CGRectMake(10, 20, 40, 40);
        [_backBtn addTarget:self action:@selector(backAct) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _backBtn;
}

-(UIButton *)editBtn
{
    if (!_editBtn) {
        _editBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        [_editBtn setTitle:@"修改" forState:(UIControlStateNormal)];
        [_editBtn setImage:[UIImage imageNamed:@"editimage"] forState:(UIControlStateNormal)];
        _editBtn.frame = CGRectMake(KscreenW-50, 30, 30, 30);
        [_editBtn addTarget:self action:@selector(EditAct) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _editBtn;
}

-(void)EditAct
{
    EditchildInfoViewController *VC = [[EditchildInfoViewController alloc]init];
    VC.sid = self.sid;
    VC.model = self.model;
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self Request];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.backImageV];
    [self.backImageV addSubview:self.backV];
    [self.view addSubview:self.listTab];
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.editBtn];

}

#pragma mark --- tableV的代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 8;
    }else
    {
        return 2;
    }
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        ChildDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChildDetailTableViewCell" forIndexPath:indexPath];
        switch (indexPath.row)
        {
            case 0:
                cell.titleL.text = @"小名";
                cell.detailL.text = self.model.nickname;
                break;
            case 1:
                cell.titleL.text = @"监护人";
                cell.detailL.text = self.model.guardian;
                break;
            case 2:
                cell.titleL.text = @"状态";
                if ([self.model.status isEqualToString:@"1"])
                {
                    cell.detailL.text = @"在读";
                }else
                {
                    cell.detailL.text = @"未读";
                }
                break;
            case 3:
                cell.titleL.text = @"在读课程";
                cell.detailL.text = self.className;
                break;
            case 4:
                cell.titleL.text = @"生日";
                cell.detailL.text = self.model.birthday;
                break;
            case 5:
                cell.titleL.text = @"住址";
                cell.detailL.text = self.model.address;
                break;
            case 6:
                cell.titleL.text = @"紧急联系人";
                cell.detailL.text = self.model.specialman;
                break;
            case 7:
                cell.titleL.text = @"紧急联系电话";
                cell.detailL.text = self.model.specialtel;
                break;
            default:
                break;
        }
        cell.selectionStyle = 0;
        return cell;
    }else
    {
        if (indexPath.row == 0)
        {
            UpRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UpRecordTableViewCell" forIndexPath:indexPath];
            cell.titleL.text = @"成长记录";
            cell.selectionStyle = 0;
            return cell;
        }else
        {
            UpRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UpRecordTableViewCell" forIndexPath:indexPath];
            cell.selectionStyle = 0;
            cell.titleL.text = @"修改信息";
            return cell;
        }
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 280;
    }else
    {
        return 10;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // 整个的头部视图
    if (section == 0)
    {
        NSString *name;
        if (self.model.name.length == 0) {
            name = @"--";
        }else
        {
            name = self.model.name;
        }
        self.headV = [[childDetailHeadV alloc]initWithFrame:CGRectMake(0, 0, KscreenW, 280)];
        self.headV.nameL.attributedText = [AttributeString string:name LineSpace:1 font:24 color:UIColors(0x333333) worldSpace:1];
        [self.headV.headimageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,self.model.lifethumb]] placeholderImage:nil];

        if ([self.model.sex isEqualToString:@"男"])
        {
            self.headV.sexImage.image = [UIImage imageNamed:@"man"];
        }else
        {
            self.headV.sexImage.image = [UIImage imageNamed:@"women"];
        }

        return self.headV;
    }else
    {
        UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, 10)];
        grayView.backgroundColor = PKCOLOR(243, 246, 246);
        return grayView;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            GrowthRecordViewController *vc = [[GrowthRecordViewController alloc]init];
            vc.sid = self.sid;
            [self.navigationController pushViewController:vc animated:YES];
        }else
        {
            EditchildInfoViewController *VC = [[EditchildInfoViewController alloc]init];
            VC.sid = self.sid;
            VC.model = self.model;
            [self.navigationController pushViewController:VC animated:YES];
        }
    }
}

-(void)Request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentInfo&a=studentDetail" parmDic:@{@"sid":self.sid} requestType:RequestGET Success:^(id data)
    {
        self.model = [ChildDetailModel modelConfigure:data];
        [self.listTab reloadData];
        [self reloadV];
        
    } error:^(NSError *error) {

    }];
}

-(void)backAct
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)reloadV
{
    [self.backImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,self.model.lifethumb]] placeholderImage:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
